package user;

import java.sql.*;

public class DisplayFoodPage {
	
	//Display different list of food for different restaurant in the customer_page
	//Read a static variable from RestaurantQuery, not safe
	public static String getFoodInfo() {
		
		DBConnector db = DBConnector.getDBConnector();		
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		String statement = "select food_name, food_price, food_description, food_picture_link from food where Rest_ID = " + Rest_ID;
		db.prepareStatement(statement);
		ResultSet FoodInfo = db.queryPstmt();
		
		return displayFoodItems(FoodInfo);		
	}	
	
	//Display the searching result in the page search_result
	public static String displayFoodSearchingResult()
	{		
		ResultSet FoodInfo = FoodSearching.getFoodSearchingInfo();
		
		return displayFoodItems(FoodInfo);
	}
	
	//only called by functions getFoodInfo and displayFoodSearchingResult
	//the display format of the food information for both the search_result.jsp and the customer_page.jsp
	private static String displayFoodItems(ResultSet FoodInfo) {
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (FoodInfo != null) {
				while (FoodInfo.next()) {
		
					sBuf.append("<table width = 100% align = center border = 1 bordercolor = #929292><tr>");
					sBuf.append( "<td width=130><a name='" + FoodInfo.getString(1)+ "'>" + 
							"<img src='Pics/" + 
							FoodInfo.getString(4) + "' width=130 height=130 border = 0></a></td><td width=200><b><i>" +
							FoodInfo.getString(1) + "</i></b><br><br>" + 
							FoodInfo.getString(3) + "</td><td>Price : $" +
							FoodInfo.getString(2) + " </td>");	
					sBuf.append("<form method='post' action='./servlet/FoodSelection' name = 'select_food'><div align='center'><td>" +
							"<input name='food_quantity' type='text' class='input' size='2' value='1'><br>");
					sBuf.append("<input name = 'food_selected' type = 'hidden' class = 'input' value = '" + FoodInfo.getString(1) + "'><br>");
					sBuf.append("<input name='SelectButton' type='submit' value='Select'></td></div></form>");
					sBuf.append("</tr></table>");
				}
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			//System.out.println("The connection is not established");
			return "";
		}finally{
		}	
	}
	
	//display the list of food of all restaurants and their sales, called in administrator_account
	public static String DisplayFoodList(String food_search, String Order_By_FoodName, String Order_By_Restaurant, String Order_By_Price, String Order_By_Sales)
	{
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();
		String queryFoods = "select food_name, rest_name, food_price, food_quantity from food, restaurant where food.rest_ID = restaurant.rest_ID ";

		if (food_search != null && food_search.length() > 0)
			queryFoods += "and  (food_name like '%" + food_search
					+ "%' or rest_name like '%" + food_search
					+ "%' or food_price like '%" + food_search + "%') ";
		if (Order_By_Restaurant != null && Order_By_Restaurant.length() > 0)
			queryFoods += "order by restaurant.rest_name ";
		else if (Order_By_Price != null && Order_By_Price.length() > 0)
			queryFoods += "order by food_price ";
		else if (Order_By_Sales != null && Order_By_Sales.length() > 0)
			queryFoods += "order by food_quantity desc";
		else if (Order_By_FoodName != null && Order_By_FoodName.length() > 0)
			queryFoods += "order by food_name ";

		try{
			db.prepareStatement(queryFoods);
			ResultSet queryFoodResult = db.queryPstmt();
			if (queryFoodResult != null) {
				sBuf.append("<tr bgColor=#cccccc><td>Food Name</td><td>Restaurant Name</td><td>Food Price</td><td># Sale</td></tr>");
				if (queryFoodResult.next()) {
					do {
						sBuf.append("<tr>");
						sBuf.append("<td>" + queryFoodResult.getString(1)
								+ "</td>");
						sBuf.append("<td>" + queryFoodResult.getString(2)
								+ "</td>");
						sBuf.append("<td>" + queryFoodResult.getString(3)
								+ "</td>");
						sBuf.append("<td>" + queryFoodResult.getString(4)
								+ "</td>");
						sBuf.append("</tr>");
					} while (queryFoodResult.next());
				} else
					sBuf.append("<tr><td colspan = 5>There are no food information fulfilling your requirement.</td></tr>");
			}
		}catch (Exception ex) {
			System.out.println("DisplayFoodPage.DisplayFoodList exception");
		}
		return sBuf.toString();
	}
	
	
	//display the list of food of all restaurants and their sales, called in restaurant_account
	public static String DisplayFoodListForRestaurant(String food_search, String Order_By_FoodName, String Order_By_Restaurant, String Order_By_Price, String Order_By_Sales, String RestID)
	{
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();
		String queryFoods = "select food_name, food_price, food_quantity from food, restaurant where food.rest_ID = restaurant.rest_ID and restaurant.rest_ID = " + RestID + " ";

		if (food_search != null && food_search.length() > 0)
			queryFoods += "and  (food_name like '%" + food_search
					+ "%' or rest_name like '%" + food_search
					+ "%' or food_price like '%" + food_search + "%') ";
		if (Order_By_Restaurant != null && Order_By_Restaurant.length() > 0)
			queryFoods += "order by restaurant.rest_name ";
		else if (Order_By_Price != null && Order_By_Price.length() > 0)
			queryFoods += "order by food_price ";
		else if (Order_By_Sales != null && Order_By_Sales.length() > 0)
			queryFoods += "order by food_quantity desc";
		else if (Order_By_FoodName != null && Order_By_FoodName.length() > 0)
			queryFoods += "order by food_name ";

		try{
			db.prepareStatement(queryFoods);
			ResultSet queryFoodResult = db.queryPstmt();
			if (queryFoodResult != null) {
				sBuf.append("<tr bgColor=#cccccc><td>Food Name</td><td>Food Price</td><td># Sale</td></tr>");
				if (queryFoodResult.next()) {
					do {
						sBuf.append("<tr>");
						sBuf.append("<td>" + queryFoodResult.getString(1)
								+ "</td>");
						sBuf.append("<td>" + queryFoodResult.getString(2)
								+ "</td>");
						sBuf.append("<td>" + queryFoodResult.getString(3)
								+ "</td>");						
						sBuf.append("</tr>");
					} while (queryFoodResult.next());
				} else
					sBuf.append("<tr><td colspan = 5>There are no food information fulfilling your requirement.</td></tr>");
			}
		}catch (Exception ex) {
			System.out.println("DisplayFoodPage.DisplayFoodListForRestaurant exception");
		}
			
		return sBuf.toString();
	}
}